package com.whsxt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.domain.Prod;
import com.whsxt.domain.Sku;
import com.whsxt.service.ProdService;
import com.whsxt.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-16 16:27
 * @Description:
 */
@RestController
@RequestMapping("/prod/prod")
@Api(tags = "商品管理接口")
public class ProdController {


    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询商品")
    public ResponseEntity<IPage<Prod>> prodPage(Page<Prod> page, Prod prod) {
        IPage<Prod> iPage = prodService.findProdPage(page, prod);
        return ResponseEntity.ok(iPage);
    }


    @PostMapping
    @ApiOperation(value = "新增商品")
    public ResponseEntity<Void> addProd(@RequestBody Prod prod) {
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }


    /**
     * 提供远程调用
     *
     * @param prodId
     * @return
     */
    @GetMapping("/findProdById")
    public Prod findProdById(@RequestParam("prodId") Long prodId) {
        return prodService.getById(prodId);
    }


    @PostMapping("/getSkuByIds")
    public List<Sku> getSkuByIds(@RequestBody List<Long> skuIds) {
        return skuService.listByIds(skuIds);
    }


    @PostMapping("/changeStock")
    @ApiOperation(value = "扣减mysql库存")
    public void changeStock(@RequestBody Map<String, Map<Long, Integer>> stockMap) {
        prodService.changeStock(stockMap);
    }


    //===============前台接口


    @GetMapping("/prod/prodInfo")
    @ApiOperation(value = "前台根据id查询商品信息（包括了sku）")
    public ResponseEntity<Prod> frontFindProdById(Long prodId) {
        Prod prod = prodService.findProdAndSkuById(prodId);
        return ResponseEntity.ok(prod);
    }





}
